<?php
/**
 * 附件管理
 */
namespace app\admin\controller;

use app\common\model\CommonAttachment as attachmentModel;
use app\util\ReturnCode;
use Upload;

class Attachment extends Base {

    /**
     * @OA\Get(
     *   path="/admin/Attachment/index",
     *   tags={"附件相关"},
     *   summary="附件列表",
     *   description="附件管理",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   @OA\Parameter(ref="#/components/parameters/keywords"),
     *   @OA\Parameter(ref="#/components/parameters/order"),
     *   @OA\Parameter(ref="#/components/parameters/size"),
     *   @OA\Parameter(ref="#/components/parameters/page"),
     *   @OA\Response(
     *     response="200",
     *     description="返回JSON",
     *     @OA\MediaType(
     *      mediaType="application/json",
     *      @OA\Schema(
     *         @OA\Property(property="code", type="number",description="状态码"),
     *         @OA\Property(property="msg", type="string",description="提示消息"),
     *         @OA\Property(property="data", type="object",description="original_name文件名，path文件路径，预览地址previewUrl",@OA\Items),
     *         example={"code":1,"msg":"获取成功","data":{"list":{{"id":19,"uid":0,"member_id":0,"original_name":"login-bg.jpg","path":"default/20200109\\5b36c263d0e17f612b2f8c0f740cef8e.jpg","type":"image/jpeg","size":460261,"hash":"d7f4dd4f55ca9dd194cd80927119e167f137fa9f","create_time":"2020-01-09 22:00:50"}},"count":1}}
     *      )
     *    )
     *   ),
     *   @OA\Response(response="500", description="服务器错误"),
     *   security={{"App-Login-Token": ""}}
     * )
     */
    public function index() {
        $limit    = $this->request->get('size', config('easyApi.ADMIN_LIST_DEFAULT'));
        $start    = $this->request->get('page', 1);
        $keywords = $this->request->get('keywords');
        $order    = $this->request->get('order','create_time DESC');
        $attachmentModel = new attachmentModel();
        if (!empty($keywords)) {
            $attachmentModel = $attachmentModel->where('original_name like "%'.$keywords.'%"');
        }
        $listObj = $attachmentModel->order($order)->paginate($limit, false, ['page' => $start])->toArray();
        $this->success('获取成功',[
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }

    /**
     * @OA\Delete(path="/admin/Attachment/delete/{id}",
     *   tags={"附件相关"},
     *   summary="删除附件",
     *   description="删除指定附件，不可恢复",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   @OA\Parameter(name="id",in="path",required=true,description="附件记录ID",@OA\Schema( type="integer", format="int32", minimum=1.0 )),
     *   @OA\Response(
     *     response=200,
     *     description="返回Json",
     *     @OA\JsonContent( type="array", @OA\Items(ref="#/components/schemas/response" ) )
     *   ),
     *   @OA\Response(response=500, description="服务器错误"),
     *   security={{"App-Login-Token": ""}}
     * )
     */
    public function delete($id) {
        $upload = new Upload();
        if(!$id){
            $this->error('参数未填');
        }
        $result = $upload->del($id);
        if($result['code'] == 1){
            $this->success($result);
        }else{
            $this->error($result);
        }
    }

    /**
     * @OA\Post(
     *   path="/admin/Attachment/add",
     *   tags={"附件相关"},
     *   summary="上传附件",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   @OA\RequestBody(
     *    @OA\MediaType(
     *      mediaType="multipart/form-data",
     *      @OA\Schema(
     *        @OA\Property( property="hash", type="string", description="文件哈希sha256散列值，多个英文逗号相隔" ),
     *        @OA\Property( property="file", type="file", description="文件" ),
     *        example={"hash": "d74d55c11416c488e3aae0883d01dfd714d86f030cd163c10aadfe77ada7e7ce", "file": ""}
     *      )
     *    )
     *  ),
     *  @OA\Response(
     *    response=200,
     *    description="OK",
     *    @OA\MediaType(
     *      mediaType="application/json",
     *      @OA\Schema(
     *         @OA\Property(property="code", type="number",description="状态码"),
     *         @OA\Property(property="msg", type="string",description="提示消息"),
     *         @OA\Property(property="data", type="object",description="fileName文件名，fileUrl文件路径，预览地址previewUrl",@OA\Items),
     *         example={"code":1,"msg":"成功","data":{{"fileName":"login-bg.jpg","fileUrl":"default\/20200107\\1bbdc52122a36c360cbd47a3760f1207.jpg","previewUrl":"http:\/\/api.myapp.com\/storage\/default\/20200107\\1bbdc52122a36c360cbd47a3760f1207.jpg"}}}
     *      )
     *    )
     *  ),
     *  @OA\Response(response=500, description="服务器错误"),
     *  security={{"App-Login-Token": ""}}
     * )
     */
    public function add() {
        $upload = new Upload();
        $result = $upload->save();
        if($result['code'] == 1){
            $this->success($result['data']);
        }else{
            $this->error($result['msg']);
        }
    }

    /**
     * @OA\Post(
     *   path="/admin/Attachment/editor",
     *   tags={"附件相关"},
     *   summary="富文本编辑器上传文件",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   @OA\RequestBody(
     *    @OA\MediaType(
     *      mediaType="multipart/form-data",
     *      @OA\Schema(
     *        @OA\Property( property="image", type="file", description="图片，name为file则是文件类型" )
     *      )
     *    )
     *  ),
     *  @OA\Response(
     *    response=200,
     *    description="OK",
     *    @OA\MediaType(
     *      mediaType="application/json",
     *      @OA\Schema(
     *         @OA\Property(property="code", type="number",description="状态码"),
     *         @OA\Property(property="msg", type="string",description="提示消息"),
     *         @OA\Property(property="data", type="object",description="fileName文件名，fileUrl文件路径，预览地址previewUrl",@OA\Items),
     *         example={"code":1,"msg":"成功","data":{"fileName":"login-bg.jpg","fileUrl":"default\/20200107\\1bbdc52122a36c360cbd47a3760f1207.jpg","previewUrl":"http:\/\/api.myapp.com\/storage\/default\/20200107\\1bbdc52122a36c360cbd47a3760f1207.jpg"}}
     *      )
     *    )
     *  ),
     *  @OA\Response(response=500, description="服务器错误"),
     *  security={{"App-Login-Token": ""}}
     * )
     */
    public function editor() {
        $upload = new Upload();
        $result = $upload->save();
        if($result['code'] == 1){
            $this->success($result['data']);
        }else{
            $this->error($result['msg']);
        }
    }
}
